package model;

import java.io.Serializable;
import java.util.ArrayList;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;

import utilities.HibernateUtil;
import entity.Slider;

public class SliderDAO {
    
    /**
     * Load Slider in Shop.
     * @return List Slider
     * @throws Exception
     */
    @SuppressWarnings({"unchecked", "finally"})
    public ArrayList<Slider> getList() throws Exception {
        ArrayList<Slider> list = new ArrayList<Slider>();
        SessionFactory sessionFactory = null;
        Session session = null;
        try {
            sessionFactory = HibernateUtil.getSessionFactory();
            session = sessionFactory.openSession();
            list = (ArrayList<Slider>) session.createCriteria(Slider.class).list();
        } catch (Exception e) {
            throw new Exception(e);
        } finally {

        }
        session.close();
        return list;
    }

    /**
     * Add New Slider
     * @param Slider
     * @throws Exception
     */
    public static void add(Slider slider) throws Exception {
        SessionFactory sessionFactory = null;
        Session session = null;
        try {
            sessionFactory = HibernateUtil.getSessionFactory();
            session = sessionFactory.openSession();
            Transaction transaction = session.beginTransaction();
            session.save(slider);
            transaction.commit();
        } catch (Exception e) {
            throw new Exception(e);
        } finally {
            session.close();
        }
    }

    /**
     * Update Slider
     * @param Slider
     * @throws Exception
     */
    public static void update(Slider slider) throws Exception {
        SessionFactory sessionFactory = null;
        Session session = null;
        try {
            sessionFactory = HibernateUtil.getSessionFactory();
            session = sessionFactory.openSession();
            Transaction transaction = session.beginTransaction();
            session.update(slider);
            transaction.commit();
        } catch (Exception e) {
            throw new Exception(e);
        } finally {
            session.close();
        }
    }

    /**
     * Delete Slider
     * @param Slider
     * @throws Exception
     */
    public static void delete(Slider slider) throws Exception {
        SessionFactory sessionFactory = null;
        Session session = null;
        try {
            sessionFactory = HibernateUtil.getSessionFactory();
            session = sessionFactory.openSession();
            Transaction transaction = session.beginTransaction();
            session.delete(slider);
            transaction.commit();
        } catch (Exception e) {
            throw new Exception(e);
        } finally {
            session.close();
        }
    }

    /**
     * Find Slider by ID
     * @param id
     * @return Slider
     * @throws Exception
     */
    @SuppressWarnings("finally")
    public static Slider find(int id) throws Exception {
        SessionFactory sessionFactory = null;
        Session session = null;
        Slider slider = new Slider();
        try {
            sessionFactory = HibernateUtil.getSessionFactory();
            session = sessionFactory.openSession();
            slider = (Slider) session.get(Slider.class, id);
        } catch (Exception e) {
            throw new Exception(e);
        } finally {
            session.close();
            return slider;
        }
    }
}
